<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;
    //指定表名
    public $table = 'posts';

    //指定主键
    public  $primarykey = 'id';

    //时间戳设置
   // protected $dateFormat  = 'U';

    //不允许批量赋值字段
    protected $guarded = ['views', 'user_id','updated_at','created_at'];

    protected $dates = ['delete_at'];

    public function scopePopular($query)
    {
        return $query->where('views','>=',100);
    }

    public function scopeStatus($query, $status = 1)
    {
        return $query->where('status',$status);
    }
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function comments()
    {
        //$this->morphMany('App\Models\Comment',$item,$item_type,$item_id,$id);
        return $this->morphMany('App\Models\Comment','item');
    }

    //多对多多态关联
    public function tags()
    {
        return $this->morphToMany('App\Models\Tag','taggable');
    }
}
